package com.wits.platform.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.wits.platform.dao.MmrRoleMenuDao;
import com.wits.platform.dao.MmrRoleResourceDao;
import com.wits.platform.dao.RoleDao;
import com.wits.platform.entity.MmrRoleMenu;
import com.wits.platform.entity.MmrRoleMenuPK;
import com.wits.platform.entity.MmrRoleResource;
import com.wits.platform.entity.Role;
@Component
public class RoleService extends BaseService<Role,String>{


	private RoleDao roleDao;

	@Resource
	private MmrRoleMenuDao mmrRoleMenuDao;
	
	@Resource
	private MmrRoleResourceDao mmrRoleResourceDao;
	
	@Resource
	public void setRoleDao(RoleDao roleDao) {
		this.baseDao=roleDao;
		this.roleDao = roleDao;
	}


	public void saveCascade(Role role,String menuIds,String resourceIds) {
		role=roleDao.save(role);
		String roleId=role.getRoleId();
		String[] menuIdArray=StringUtils.split(menuIds, ",");
		String[] rIdArray=StringUtils.split(resourceIds, ",");
		MmrRoleResource roleResource=null;
		for(String rscId : rIdArray){
			roleResource=new MmrRoleResource(roleId,rscId);
			mmrRoleResourceDao.save(roleResource);
		}
		MmrRoleMenu roleMenu=null;
		for(String menuId:menuIdArray){
			roleMenu=new MmrRoleMenu(new MmrRoleMenuPK(menuId,roleId));
			mmrRoleMenuDao.save(roleMenu);
		}
		
	}
	
	public void updateCascade(Role command,String menuIds,String resourceIds) {
		String roleId=command.getRoleId();
		mmrRoleResourceDao.deleteByRoleId(roleId);
		mmrRoleMenuDao.deleteByRoleId(roleId);
		this.saveCascade(command, menuIds, resourceIds);
	}


	@Override
	public void deleteInBatch(List<String> idList) {
		roleDao.executeUpdate("delete from MmrUserRole ur where ur.id.roleId in(?1)", idList);
		super.deleteInBatch(idList);
	}
	


}
